//
//  HomeView.swift
//  jingxin
//
//  Created by Yuki on 2020/8/7.
//  Copyright © 2020 Zl. All rights reserved.
//

import UIKit

class HomeView: BaseView, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource,UICollectionViewDelegate ,UIScrollViewDelegate{
    lazy var ary = ["关注","推荐"];//数据源数组
    lazy var selectedIndex = NSIndexPath.init(row: 0, section: 0);//默认第一个是选中的
    var isScroll:Bool?;//是否正在滑动中
    
    override init(frame: CGRect) {
        super.init(frame: frame);
        isScroll = false;
        buildUI();//创建界面
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder);
        self.ary = ["关注","推荐"];
        isScroll = false;
        buildUI();//创建界面
    }
    
    
    //MARK: 创建界面
    private func buildUI(){
        self.backgroundColor = Color(0xffbb00, 1);
        self.addSubview(self.menu_collecView);
        self.menu_collecView.snp_makeConstraints { (make) in
            make.left.equalTo(self.snp_left).offset(11*width());
            make.top.equalTo(self.snp_top).offset(54*height());
            make.size.equalTo(CGSize(width: 145*width(), height: 24*height()));
        }
        
        /**  搜索框  **/
        self.addSubview(self.searchView);
        self.searchView.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 208*width(), height: 42*height()));
            make.right.equalTo(-11*width());
            make.centerY.equalTo(self.menu_collecView);
        };
        
        /**  点击搜索框  **/
        self.searchView.clickBlockContentParam = { (searchView) in
            
        }
        
    }
    
    //MARK:set方法
    //设置默认搜索关键字
    var placeholderStr:String?{
        didSet{
            self.searchView.placeholderStr = placeholderStr ?? "搜索关键字推荐你"
        }
    }
    
    /**  获取当前滑动位置  **/
    var beginIndex:Int?{
        didSet{
            self.selectedIndex = NSIndexPath.init(row: self.beginIndex ?? 0, section: 0);
            self.menu_collecView.reloadData();
        }
    }
    
    // 设置桥接scroll
    var bridgeScro:UIScrollView?{
        didSet{
            self.bridgeScro?.delegate = self;//设置滑动视图的代理
            /**  设置初始便宜位置  **/
            self.bridgeScro?.setContentOffset(CGPoint(x: CGFloat(Float(self.beginIndex ?? 0)) * ScreenW(), y: 0), animated: true);
        }
    }
    
    //MARK: scrollview代理
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView == self.bridgeScro{
            self.isScroll = true;
            let currentRow = (self.bridgeScro?.contentOffset.x)!/ScreenW();
            self.beginIndex = Int(currentRow);
        }
    }
    /**  桥接scroll滑动结束了  **/
    func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
        if  scrollView == self.bridgeScro {
            print("结束滑动了");
            isScroll = false;
        }
    }
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        if scrollView == self.bridgeScro {
            print("拖动结束了");
            isScroll = false;
        }
    }
    
    
    //MARK:CollectionView代理
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if !self.ary.isEmpty {
            return self.ary.count;
        }
        return 0;
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1;
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 22.0;
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 2.0;
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 40*width(), height: 24*height());
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSize.zero;
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize.zero
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let item:Home_MenuItem = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(Home_MenuItem.classForCoder()), for: indexPath) as! Home_MenuItem;
        if indexPath.row == selectedIndex.row{
            item.isSelected = true;
        }else{
            item.isSelected = false;
        }
        item.titleStr = self.ary[indexPath.row];
        return item
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.selectedIndex = indexPath as NSIndexPath;
        self.menu_collecView.reloadData();
        
        /**  如果不在滑动中  **/
        if !isScroll! {
            self.bridgeScro?.setContentOffset(CGPoint.init(x: CGFloat(ScreenW() * CGFloat(indexPath.row)), y: 0), animated: true);
        }
    }
    
    
    //MARK:=======lazy
    /**  关注和推荐  **/
    lazy var menu_collecView :UICollectionView = {
        let collectionFlowlayout = UICollectionViewFlowLayout();
        collectionFlowlayout.scrollDirection = .horizontal;
        var menu_collecView = UICollectionView(frame: CGRect.zero, collectionViewLayout: collectionFlowlayout)
        menu_collecView.backgroundColor = UIColor.clear;
        menu_collecView.delegate = self;
        menu_collecView.dataSource = self;
        menu_collecView.register(Home_MenuItem.classForCoder(), forCellWithReuseIdentifier: NSStringFromClass(Home_MenuItem.classForCoder()));
        return menu_collecView;
    }();
    
    
    /**  搜索框  **/
    lazy var searchView :SearchView = {
        var searchView = SearchView.init();
        searchView.backgroundColor = UIColor.white;
        searchView.layer.cornerRadius = 21.0*height();
        searchView.layer.masksToBounds = true;
        searchView.canEdit = false;
        searchView.placeholderStr = "搜索关键字推荐";
        return searchView;
    }();
    
}
